package com.sf.fy.uav.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sf.fy.uav.common.enums.ScenicSpotStatusEnum;
import com.sf.fy.uav.common.request.operate.ScenicSpotReq;
import com.sf.fy.uav.common.response.operational.ScenicSpotRes;
import com.sf.fy.uav.common.utils.RespJsonPageData;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.withdrawal.entity.ScenicSpot;
import com.sf.fy.uav.withdrawal.service.IScenicSpotService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lixiang
 * @since 2023-09-12
 */
@RestController
@RequestMapping("/sys/scenicSpot")
@RequiredArgsConstructor
public class ScenicSpotRest {

    private final IScenicSpotService iScenicSpotService;

    @GetMapping("select-by-id")
    public ScenicSpot selectById(String id) {
        return iScenicSpotService.selectById(id);
    }

    public RespJsonPageData<ScenicSpotRes> getPageScenicSpot(ScenicSpotReq scenicSpotReq) {
        return iScenicSpotService.getPageScenicSpot(scenicSpotReq);
    }

    public Rs<Integer> addPageScenicSpot(ScenicSpotReq scenicSpotReq) {
       return iScenicSpotService.save(scenicSpotReq);
    }

    public Rs<Integer> editScenicSpot(ScenicSpotReq scenicSpotReq) {
        return iScenicSpotService.updateById(scenicSpotReq);
    }

    public List<ScenicSpot> selectScenicSpotListByIds (Set<String> ids){
        return iScenicSpotService.list(new QueryWrapper<ScenicSpot>().in("id",ids).eq("deleted",false));
    }

    public Rs<Map<String,String>> queryScenicSpotName(String id,String name){
        LambdaQueryWrapper<ScenicSpot> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(name) ,ScenicSpot::getName,name)
                .eq(StringUtils.isNotBlank(id),ScenicSpot::getId,id)
                .eq(ScenicSpot::getDeleted,false);
         List<ScenicSpot> scenicSpotList = iScenicSpotService.list(wrapper);
        return Rs.success(scenicSpotList.stream().collect(Collectors.toMap(ScenicSpot::getId,ScenicSpot::getName,(value1, value2) -> value1)));
    }

    public Rs<List<ScenicSpot>> queryScenicSpotNameTwo(String id,String name){
        LambdaQueryWrapper<ScenicSpot> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(name) ,ScenicSpot::getName,name)
                .eq(StringUtils.isNotBlank(id),ScenicSpot::getId,id)
                .eq(ScenicSpot::getStatus, ScenicSpotStatusEnum.NORMAL.getValue())
                .eq(ScenicSpot::getDeleted,false);
        return Rs.success(iScenicSpotService.list(wrapper));
    }
    public ScenicSpot selectByIdNoByStatus(String id) {
        return iScenicSpotService.getById(id);
    }
    public Rs<List<ScenicSpot>> queryScenicSpotNameThree(String id, String name) {
        LambdaQueryWrapper<ScenicSpot> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(name) ,ScenicSpot::getName,name)
                .eq(StringUtils.isNotBlank(id),ScenicSpot::getId,id)
                .eq(ScenicSpot::getDeleted,false);
        return Rs.success(iScenicSpotService.list(wrapper));
    }
}
